//
//  LLKCollectionManage.m
//
//
//  Created by QianFeng on 16/5/9.
//  Copyright © 2016年 李龙坤. All rights reserved.
//

#import "LLKCollectionManage.h"
#import <FMDatabase.h>
@interface LLKCollectionManage ()
//数据库
@property (nonatomic,strong)FMDatabase *dataBase;
//数据库路径
@property (nonatomic,copy)NSString * dbPath;
@end
@implementation LLKCollectionManage
//单例
+ (instancetype)shareManager {
    static LLKCollectionManage * mgr = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mgr = [[LLKCollectionManage alloc] init];
    });
    return mgr;
}
- (instancetype)init {
    self = [super init];
    if (self) {
        //创建数据库
        _dataBase = [FMDatabase databaseWithPath:self.dbPath];
        //如果能打开数据库
        if ([_dataBase open]) {
            NSString *sql = @"create table if not exists appdetail(userName text,userPhonenumber text,userCallTime text,userAddress text)";
            if ([_dataBase executeUpdate:sql]) {
                NSLog(@"成功");
                
            } else {
                NSLog(@"失败");
            }
            //关闭数据，
            [_dataBase close];
        }
    }
    return self;
}

- (NSString *)dbPath {
    if (_dbPath == nil) {
        //获取沙盒路劲
        NSString * path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)lastObject];
        _dbPath = [path stringByAppendingPathComponent:@"long11.sqlite"];
    }
    return _dbPath;
}

//添加收藏
- (BOOL)addCollect:(CallModel *)user {
    
    BOOL success;
    
   NSString *sql = @"insert into appdetail(userName,userPhonenumber,userCallTime,userAddress) values(?,?,?,?)";
    if ([_dataBase open]) {
        if ([_dataBase executeUpdate:sql,user.userName,user.userPhonenumber,user.userCallTime,user.userAddress]) {
            success = YES;
            NSLog(@"收藏成功");
        } else {
            success = NO;
            NSLog(@"收藏失败");
        }
        [_dataBase close];
    }
    
    return success;
}


- (NSMutableArray *)selectAll {
    NSMutableArray * array = [[NSMutableArray alloc] init];
    if ([_dataBase open]) {
        NSString * sql = @"select * from appdetail";
        //查询
        FMResultSet * res = [_dataBase executeQuery:sql];
        //遍历
        while ([res next]) {
            CallModel * user = [[CallModel alloc] init];
            user.userName = [res stringForColumn:@"userName"];
            user.userPhonenumber = [res stringForColumn:@"userPhonenumber"];
            user.userCallTime = [res stringForColumn:@"userCallTime"];
            user.userAddress = [res stringForColumn:@"userAddress"];
            [array addObject:user];
        }
        //关闭
        [_dataBase close];
    }
    
    return array;
}
//判断是否存在
- (BOOL)APPIsExist:(CallModel *)user{
    BOOL result = NO;
    
    NSString * sql = @"select * from appdetail where userPhonenumber = ?";
    //查询
    if ([_dataBase open]) {
        FMResultSet * res = [_dataBase executeQuery:sql,user.userPhonenumber];
        //遍历
        while ([res next]) {
            //如果能进入循环，就说明已经存在
            result = YES;
        }
        [_dataBase close];
    }
    
    return result;

}
//从表中获取用户信息
- (CallModel *)getUserByuserPhonenumebr:(NSString *)userPhonenumebr {
    __block CallModel *model = nil;
     NSString * sql = @"select * from appdetail where userPhonenumber = ?";
    if ([_dataBase open]) {
         FMResultSet * res = [_dataBase executeQuery:sql,userPhonenumebr];
        while ([res next]) {
            model = [[CallModel alloc] init];
            model.userName = [res stringForColumn:@"userName"];
            model.userPhonenumber = [res stringForColumn:@"userPhonenumber"];
            model.userCallTime = [res stringForColumn:@"userCallTime"];
            model.userAddress = [res stringForColumn:@"userAddress"];
        }
        [_dataBase close];
    }
    return model;
}
- (BOOL)deleteByuserPhonenumebr:(NSString *)userPhonenumebr {
    BOOL success;
    
    if ([_dataBase open]) {
        NSString * sql = @"delete from appdetail where userPhonenumber = ?";
        if ([_dataBase executeUpdate:sql,userPhonenumebr]) {
            success = YES;
        }else{
            success = NO;
        }
        
        [_dataBase close];
    }
    
    return success;
}



@end
